<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-row type="flex" justify="end">
        <el-button type="primary" @click="showFn('添加员工')">添加员工</el-button>
      </el-row>
      <br>
      <el-card class="box-card">
        <el-table
          height="calc(100vh - 310px)"
          :data="tableData"
          style="width: 100%"
          row-key="id"
        >
          >
          <el-table-column
            prop="username"
            label="用户名"
          />
          <el-table-column
            prop="email"
            label="邮箱"
          />
          <el-table-column
            label="操作"
            width="300"
          >
            <template v-slot="{row}">
              <el-button
                type="primary"
                size="mini"
                @click="shezhi(row.id)"
              >设置角色</el-button>
              <el-button
                size="mini"
                @click="showFn('编辑员工',row.id)"
              >编辑</el-button>
              <el-button size="mini" type="danger" @click="del(row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
        <br>
        <!-- 分页器 -->
        <!-- <el-row type="flex" justify="center">
          <el-pagination
            layout="total"
            :total="total"
          />
        </el-row> -->
        <FenYeQi :total="total" :page.sync="page" :size.sync="size" @handleChange="handleChange" />
      </el-card>
      <!-- 添加,编辑 -->
      <el-dialog :title="title" :visible="dialogShow" :before-close="cloFn">
        <el-form ref="jueseForm" :model="form">
          <el-form-item label="用户名" prop="username" :label-width="formLabelWidth">
            <el-input v-model="form.username" autocomplete="off" />
          </el-form-item>
          <el-form-item label="邮 箱" prop="email" :label-width="formLabelWidth">
            <el-input v-model="form.email" autocomplete="off" />
          </el-form-item>
          <el-form-item label="密 码" prop="password" :label-width="formLabelWidth">
            <el-input v-model="form.password" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogShow = false">取 消</el-button>
          <el-button type="primary" @click="butOk">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分配权限 -->
      <el-dialog
        title="权限列表"
        :visible="quanxianVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-checkbox-group v-model="checklist">
          <el-tree
            :data="jueseData"
            :props="{label: 'title',children: 'children'}"
            default-expand-all
            :expand-on-click-node="false"
          >
            <template #default="{data}">
              <el-checkbox :label="data.id">
                {{ data.title }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <div slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="jueseOk">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getYuangongList,
  getYuangongCount,
  getYuangongById,
  addYuangong,
  editYuangong,
  delYuangong } from '@/api/yuangong'
import { getJueseList } from '@/api/juese'
export default {
  data() {
    return {
      formLabelWidth: '120px',
      dialogShow: false,
      quanxianVisible: false,
      tableData: [],
      form: {
        username: 'yizu111',
        email: '103118688@qq.com',
        password: '123456'
      },
      jueseData: [],
      total: 0,
      page: 1,
      size: 8,
      title: '',
      checklist: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  async created() {
    this.getYuangongListFn(this.size, this.page)
    this.jueseData = await getJueseList()
  },
  methods: {
    async getYuangongListFn(size, page) {
      console.log(size, size * (page - 1))
      const res = await getYuangongList(size, size * (page - 1))
      this.total = await getYuangongCount()
      this.tableData = res
    },
    async showFn(val, id) {
      this.dialogShow = true
      this.title = val
      if (id) {
        this.form = await getYuangongById(id)
      }
    },
    async butOk() {
      if (this.title === '添加员工') {
        await addYuangong(this.form)
      } else if (this.title === '编辑员工') {
        await editYuangong(this.form)
      }
      this.cloFn()
      this.getYuangongListFn(this.size, this.page)
    },
    cloFn() {
      this.dialogShow = false
      this.$refs.jueseForm.resetFields()
      this.form = {}
    },
    async shezhi(id) {
      if (id) {
        this.form = await getYuangongById(id)
        this.checklist = this.form.roles.map(v => v.id)
      }
      this.quanxianVisible = true
    },
    // 设置角色
    async jueseOk() {
      this.form.roles = this.jueseData.filter(v => this.checklist.indexOf(v.id) !== -1)
      await editYuangong(this.form)
      this.handleClose()
      await this.getYuangongListFn(this.size, this.page)
    },
    handleClose() {
      this.quanxianVisible = false
    },
    // 删除员工
    async del(id) {
      if (confirm('确定删除吗?')) {
        console.log('删除')
        await delYuangong(id)
        await this.getYuangongListFn(this.size, this.page)
      }
    },
    // 分页器
    async handleChange() {
      await this.getYuangongListFn(this.size, this.page)
    }
  }
}
</script>

<style lang='less' scoped >

</style>
